% 例6.1 使用傅里叶级数将周期函数转换为不同正弦波的叠加。
A=2;
x=-5:0.1:5;
%前一次谐波叠代
hold on;
fx1=A/2+2*A/pi*(sin(1*pi*x/2)/1);
fxdd1=fx1;
subplot(2,3,1),plot(x,fxdd1,'k'),xlabel('(a)前一次谐波迭代');

%前两次谐波叠代
hold on;
fx2=A/2+2*A/pi*(sin(1*pi*x/2)/1+sin(3*pi*x/2)/3);
fxdd2=fx1+fx2;
subplot(2,3,2),plot(x,fxdd2,'k'),xlabel('(b)前两次谐波迭代');

%前三次谐波叠代
hold on;
fx3=A/2+2*A/pi*(sin(1*pi*x/2)/1+sin(3*pi*x/2)/3+sin(5*pi*x/2)/5);
fxdd3=fx1+fx2+fx3;
subplot(2,3,3),plot(x,fxdd3,'k'),xlabel('(c)前三次谐波迭代');

%前四次谐波叠代
hold on;
fx4=A/2+2*A/pi*(sin(1*pi*x/2)/1+sin(3*pi*x/2)/3+sin(5*pi*x/2)/5+sin(7*pi*x/2)/7);
fxdd4=fx1+fx2+fx3+fx4;
subplot(2,3,4),plot(x,fxdd4,'k'),xlabel('(d)前四次谐波迭代');

%前五次谐波叠代
hold on;
fx5=A/2+2*A/pi*(sin(1*pi*x/2)/1+sin(3*pi*x/2)/3+sin(5*pi*x/2)/5+sin(7*pi*x/2)/7+sin(9*pi*x/2)/9);
fxdd5=fx1+fx2+fx3+fx4+fx5;
subplot(2,3,5),plot(x,fxdd5,'k'),xlabel('(e)前五次谐波迭代');

%前四十次谐波叠代
hold on;
syms n;
k=symsum(2*A/pi*sin((2*n+1)*pi*x/2)/(2*n+1),0,39);
fxdd80=A/2+subs(k);
subplot(2,3,6),plot(x,fxdd80,'k'),xlabel('(f)前40次谐波迭代'),box off;